<?php

namespace Biz\service\Album;


use Biz\model\Album\AlbumTagClassifyMappingModel;
use Biz\model\ShopCategoryClassify;

class AlbumTagClassifyMappingService
{

    /**
     * @param $classifyId
     * @param $tagNames
     * @param $adminId
     * @return bool
     */
    public static function saveTagMapping($classifyId, $tagNames, $adminId): bool
    {
        if ($classifyId > 0) {

            if (!empty($tagNames)) {
                AlbumTagClassifyMappingModel::updateDataByWhere(
                    ['status' => AlbumTagClassifyMappingModel::STATUS_DELETE, 'admin_id' => $adminId],
                    ['tag[!]' => $tagNames, 'classify_id' => $classifyId]
                );
                foreach ($tagNames as $tag) {
                    $data = [
                        'status' => AlbumTagClassifyMappingModel::STATUS_NORMAL,
                        'admin_id' => $adminId,
                        'tag' => $tag,
                        'classify_id' => $classifyId,
                    ];
                    $rowCount = AlbumTagClassifyMappingModel::updateByWhere(
                        $data,
                        ['tag' => $tag, 'classify_id' => $classifyId]
                    );
                    if ($rowCount == 0) {
                        AlbumTagClassifyMappingModel::insertData($data);
                    }

                }
            } else {
                AlbumTagClassifyMappingModel::updateDataByWhere(
                    ['status' => AlbumTagClassifyMappingModel::STATUS_DELETE, 'admin_id' => $adminId],
                    ['classify_id' => $classifyId]
                );
            }
        }

        return true;

    }

    /**
     * @param $allCategoryTree
     * @return array
     */
    public static function getClassifyList($allCategoryTree): array
    {
        $result = [];
        if ($allCategoryTree) {
            foreach ($allCategoryTree as $firstInfo) {
                $firstCateName = $firstInfo['classify_name'];
                $result[$firstInfo['id']] = $firstCateName;
                if (isset($firstInfo['children']) && $firstInfo['children']) {
                    foreach ($firstInfo['children'] as $secondInfo) {
                        $secondCateName = $secondInfo['classify_name'];
                        $result[$secondInfo['id']] = $firstCateName.'>>'.$secondCateName;
                        if (isset($secondInfo['children']) && $secondInfo['children']) {
                            foreach ($secondInfo['children'] as $thirdInfo) {
                                $thirdCateName = $thirdInfo['classify_name'];
                                $result[$thirdInfo['id']] = $firstCateName.'>>'.$secondCateName.'>>'.$thirdCateName;
                            }
                        }
                    }
                }
            }
        }

        return $result;
    }

    /**
     * @param array $data
     * @param int $parent_id
     * @param int $level
     * @return array
     */
    public static function getTree(array $data = [], int $parent_id = 0, int $level = 0): array
    {
        $tree = [];
        if ($data && is_array($data)) {
            foreach ($data as $v) {
                if ($v['parent_id'] == $parent_id) {
                    $tree[] = [
                        'id' => $v['id'],
                        'classify_name' => $v['classify_name'],
                        'parent_id' => $v['parent_id'],
                        'children' => self::getTree($data, $v['id'], $level + 1),
                    ];
                }
            }
        }

        return $tree;
    }
}